from django.shortcuts import render

# 开发框架中通过中间件默认是需要登录态的，如有不需要登录的，可添加装饰器login_exempt
# 装饰器引入 from blueapps.account.decorators import login_exempt



from django.shortcuts import render, get_object_or_404, redirect
from .models import ScenicSpot, QaPair, DataTicket, DataChange, Admin, Submitter
from django.http import HttpResponse, JsonResponse
from django.contrib import messages
from django.utils import timezone

# 主界面
def home(request):
    """
    首页
    """
    return render(request, 'qa/index_home.html')

# ScenicSpot views
def scenic_spot_list(request):
    # admins = Admin.objects.all()
    # scenics = ScenicSpot.objects.all()
    # scenic_ids = list(scenics.values('jq_id', 'jq_name'))
    # return render(request, 'qa/admin/index.html', {'admins': admins, 'scenics': scenic_ids})
    #
    scenic_spots = ScenicSpot.objects.all()
    return render(request, 'qa/scenic_spot/index.html', {'scenic_spots': scenic_spots})






def scenic_spot_detail(request, jq_id):
    scenic_spot = get_object_or_404(ScenicSpot, jq_id=jq_id)
    return render(request, 'qa/scenic_spot_detail.html', {'scenic_spot': scenic_spot})

def scenic_spot_create(request):
    if request.method == 'POST':
        name = request.POST.get('jq_name')
        city = request.POST.get('jq_city')
        address = request.POST.get('jq_address')
        rating = request.POST.get('jq_rating')
        spot_type = request.POST.get('jq_type')

        new_spot = ScenicSpot(jq_name=name, jq_city=city, jq_address=address, jq_rating=rating, jq_type=spot_type)
        new_spot.save()
        messages.success(request, "新景区创建成功！")
        return redirect('scenic_spot_list')
    return render(request, 'qa/scenic_spot/create.html')


def scenic_spot_update(request, jq_id):
    scenic_spot = get_object_or_404(ScenicSpot, jq_id=jq_id)
    if request.method == 'POST':
        scenic_spot.jq_name = request.POST.get('jq_name')
        scenic_spot.jq_city = request.POST.get('jq_city')
        scenic_spot.jq_address = request.POST.get('jq_address')
        scenic_spot.jq_rating = request.POST.get('jq_rating')
        scenic_spot.jq_type = request.POST.get('jq_type')
        scenic_spot.save()
        messages.success(request, "景区信息更新成功！")
        return redirect('scenic_spot_detail', jq_id=jq_id)
    return render(request, 'qa/scenic_spot_update.html', {'scenic_spot': scenic_spot})

def scenic_spot_delete(request, jq_id):
    scenic_spot = get_object_or_404(ScenicSpot, jq_id=jq_id)
    scenic_spot.delete()
    messages.success(request, "景区删除成功！")
    return redirect('scenic_spot_list')

# QaPair views
def qa_pair_list(request):
    qa_pairs = QaPair.objects.all()
    return render(request, 'qa/qa_pair/index.html', {'qa_pairs': qa_pairs})


def qa_pair_create(request):
    if request.method == 'POST':
        question = request.POST.get('question')
        answer = request.POST.get('answer')
        scenic_spot = get_object_or_404(ScenicSpot, jq_id=request.POST.get('jq_id'))

        new_qa_pair = QaPair(question=question, answer=answer, jq=scenic_spot)
        new_qa_pair.save()
        messages.success(request, "问答对创建成功！")
        return redirect('qa_pair_list')
    scenic_spots = ScenicSpot.objects.all()
    return render(request, 'qa/qa_pair_create.html', {'scenic_spots': scenic_spots})

def qa_pair_update(request, qa_id):
    qa_pair = get_object_or_404(QaPair, qa_id=qa_id)
    if request.method == 'POST':
        qa_pair.question = request.POST.get('question')
        qa_pair.answer = request.POST.get('answer')
        qa_pair.save()
        messages.success(request, "问答对更新成功！")
        return redirect('qa_pair_list')
    return render(request, 'qa/qa_pair_update.html', {'qa_pair': qa_pair})

def qa_pair_delete(request, qa_id):
    qa_pair = get_object_or_404(QaPair, qa_id=qa_id)
    qa_pair.delete()
    messages.success(request, "问答对删除成功！")
    return redirect('qa_pair_list')

# DataTicket & DataChange views
def submit_ticket(request):
    if request.method == 'POST':
        ticket_type = request.POST.get('ticket_type')
        question = request.POST.get('question')
        answer = request.POST.get('answer')
        submitter = get_object_or_404(Submitter, submitter_id=request.user.id)
        new_ticket = DataTicket(ticket_type=ticket_type, question=question, answer=answer, submitter=submitter)
        new_ticket.save()
        messages.success(request, "内容提交成功！")
        return redirect('data_ticket_list')
    return render(request, 'qa/submit_ticket.html')

def review_ticket(request, ticket_id):
    ticket = get_object_or_404(DataTicket, ticket_id=ticket_id)
    if request.method == 'POST':
        ticket.status = request.POST.get('status')
        ticket.save()
        messages.success(request, "审核完成！")
        # If approved, create a DataChange entry
        if ticket.status == '已审核':
            change = DataChange(
                qa=ticket.qa,
                current_status='on',
                modification_type='审核',
                original_question=ticket.question,
                original_answer=ticket.answer,
                change_time=timezone.now(),
                modified_by=request.user.id,
                ticket=ticket
            )
            change.save()
        return redirect('data_ticket_list')
    return render(request, 'qa/review_ticket.html', {'ticket': ticket})



# 管理员名单展示界面
# ----------------------------------------------------------------------------------
def administrators(request):
    admins = Admin.objects.all()
    scenics = ScenicSpot.objects.all()
    scenic_ids = list(scenics.values('jq_id', 'jq_name'))
    return render(request, 'qa/admin/index.html', {'admins': admins, 'scenics': scenic_ids})

from django.template.defaultfilters import register

@register.filter(name='get_scenic_name')
def get_scenic_name(value, scenic_list):
    scenic_names = {scenic['jq_id']: scenic['jq_name'] for scenic in scenic_list}
    return scenic_names.get(value, '未知')
# ----------------------------------------------------------------------------------


def submitter(request):
    return render(request, 'qa/submitter/index.html')

# 提交数据提单
#  ---------------------------------------------------------
def my_apply(request):
    return render(request, 'qa/submitter/my_apply.html')

# 搜索问答对
def search_qa_pair(request):
    if request.method == 'POST':
        query = request.POST.get('query', '')
        result = QaPair.objects.filter(question__icontains=query).first()
        if result:
            return JsonResponse({'qa': {'question': result.question, 'answer': result.answer}})
        else:
            return JsonResponse({'qa': None})


# 提交数据提单
def submit_ticket(request):
    if request.method == 'POST':
        # 处理提交的数据提单
        pass
    return redirect('submit_ticket')  # 提交后重定向到数据提单查看页面

# ---------------------------------------------------------------


def my_check(request):
    return render(request, 'qa/admin/my_check.html')




# from django.template.defaultfilters import register
#
# @register.filter(name='get_scenic_name')
# def get_scenic_name(value, scenic_ids):
#     scenic_names = {scenic['jq_id']: scenic['jq_name'] for scenic in scenic_ids}
#     return scenic_names.get(value, '')





